package com.yny.administrator.android1607_meishi.avtivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.yny.administrator.android1607_meishi.R;
import com.yny.administrator.android1607_meishi.utils.VideoDao;

public class ContentsActivity extends AppCompatActivity {

    private WebView mWv;
    public static VideoDao mDao;
    public static SQLiteDatabase mDb;
    private String desc;
    private boolean flag = true;
    private ImageView ivCol;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_contents);
        Intent intent = getIntent();
        mDao = new VideoDao(this);
        mDb = mDao.getDb();

        final String STRING_URL = intent.getStringExtra("url");
        desc = intent.getStringExtra("recommend_caption");
        mWv = (WebView) findViewById(R.id.webview);
        ivCol = (ImageView) findViewById(R.id.col);
        Cursor cursor = mDb.query("movie", new String[]{"desc"}, "desc = ? ", new String[]{desc}, null, null, null);
        if (cursor.moveToNext()){
            ivCol.setImageResource(R.drawable.collect);
            flag=false;
        }
        WebSettings settings = mWv.getSettings();
        //支持javascript脚本功能
        settings.setJavaScriptEnabled(true);
        //是否支持缩放
        settings.setSupportZoom(true);
        //显示缩放控制器
        settings.setDisplayZoomControls(true);
        mWv.loadUrl(STRING_URL);
        mWv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(STRING_URL);
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

    }
    public void colORdel(View view){
        if (flag){
            ContentValues values=new ContentValues();
            values.put("desc",desc);
            long l = mDb.insert("movie", null, values);
            if (l>0){
                Toast.makeText(ContentsActivity.this, "收藏成功", Toast.LENGTH_LONG).show();
                ivCol.setImageResource(R.drawable.collect);
            }
            flag =false;
        }else {
            int i = mDb.delete("movie", "desc = ?", new String[]{desc});
            if (i>=0){
                Toast.makeText(ContentsActivity.this, "取消收藏", Toast.LENGTH_LONG).show();
                ivCol.setImageResource(R.drawable.cn_col01);
            }
            flag=true;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch(keyCode){
            case  KeyEvent.KEYCODE_BACK:
                if (mWv!=null&&mWv.canGoBack()){
                    mWv.goBack();
                }else {
                    finish();
                }
                break;
        }
        return false;
    }

    public void back(View view) {
        onBackPressed();
    }
}
